<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Sidebar_library
{
	
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		if (!isset($this->CI))
		{
			$this->CI =& get_instance();
		}
		
		$this->CI->config->load('database_tables', TRUE);
		
		$this->_table = $this->CI->config->item('database_tables');
		
	}

	
	// Public methods
	public function get_right_sidebar_items()
	{
		$this->CI->db->select('title, file, status, position');
		$this->CI->db->where('status', '1');
		$this->CI->db->where('type', '0');
		$this->CI->db->order_by('position', 'ASC');
		
		$query = $this->CI->db->get($this->_table['sidebar']);

		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();

			return $result;
		}
	}
	public function get_left_sidebar_items()
	{
		$this->CI->db->select('title, file, status, position');
		$this->CI->db->where('status', '1');
		$this->CI->db->where('type', '1');
		$this->CI->db->order_by('position', 'ASC');
		
		$query = $this->CI->db->get($this->_table['sidebar']);

		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();

			return $result;
		}
	}
	
	public function get_featured_news()
	{
		$this->CI->db->select('site_content.id, site_content.alias, site_content.description, site_content.category_id, site_content.content_image, site_content.title, site_content.status, site_content.featured, site_content.content, site_category.id as site_categoryid, site_category.alias as site_categoryalias');
		$this->CI->db->from('site_content','site_category');
		$this->CI->db->join('site_category','site_category.id = site_content.category_id');
		$this->CI->db->where('site_content.status', '1');
		$this->CI->db->where('site_content.featured', '1');
		$this->CI->db->order_by('site_content.id', 'DESC');
		$this->CI->db->limit(5);
		$query = $this->CI->db->get();
		if($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}
	
	public function get_lastet_post_by_cat($id,$limit=6)
	{
		$this->CI->db->select('site_content.id, site_content.alias, site_content.description, site_content.category_id, site_content.content_image, site_content.title, site_content.status, site_content.featured, site_content.content, site_category.id as site_categoryid, site_category.alias as site_categoryalias');
		$this->CI->db->from('site_content','site_category');
		$this->CI->db->join('site_category','site_category.id = site_content.category_id');
		$this->CI->db->where('site_content.status', '1');
		$this->CI->db->where('site_category.id', $id);
		$this->CI->db->order_by('site_content.id', 'DESC');
		$this->CI->db->limit($limit);
		$query = $this->CI->db->get();
		if($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}
	public function get_slideshows()
	{
		return $this->get_lastet_post_by_cat(14,6);
	}
	public function get_services()
	{
		return $this->get_lastet_post_by_cat(13,5);
	}
	
	public function get_lastest_vh()
	{
		return $this->get_lastet_post_by_cat(10);
	}
	public function get_lastest_news()
	{
		$this->CI->db->select('site_content.id, site_content.alias, site_content.description, site_content.category_id, site_content.content_image, site_content.title, site_content.status, site_content.featured, site_content.content, site_category.id as site_categoryid, site_category.alias as site_categoryalias');
		$this->CI->db->from('site_content','site_category');
		$this->CI->db->join('site_category','site_category.id = site_content.category_id');
		$this->CI->db->where('site_content.status', '1');
		$this->CI->db->where('site_category.id', '1');
		$this->CI->db->or_where('site_category.id', '9');
		$this->CI->db->order_by('site_content.id', 'DESC');
		$this->CI->db->limit(6);
		$query = $this->CI->db->get();
		if($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}
	
	public function get_tinhoatdong()
	{
		$this->CI->db->select('site_content.id, site_content.alias, site_content.description, site_content.category_id, site_content.content_image, site_content.title, site_content.status, site_content.featured, site_content.content, site_category.id as site_categoryid, site_category.alias as site_categoryalias');
		$this->CI->db->from('site_content','site_category');
		$this->CI->db->join('site_category','site_category.id = site_content.category_id');
		$this->CI->db->where('site_content.status', '1');
		$this->CI->db->order_by('site_content.id', 'DESC');
		$this->CI->db->where('site_category.id', '3');
		$this->CI->db->limit(5);
		$query = $this->CI->db->get();
		if($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}
	
	public function get_thongbao()
	{
		$this->CI->db->select('site_content.id, site_content.alias, site_content.description, site_content.category_id, site_content.title, site_content.status, site_category.id as site_categoryid, site_category.alias as site_categoryalias');
		$this->CI->db->from('site_content','site_category');
		$this->CI->db->join('site_category','site_category.id = site_content.category_id');
		$this->CI->db->where('site_content.status', '1');
		$this->CI->db->where('site_content.category_id', 2);
		$this->CI->db->order_by('site_content.id', 'DESC');
		$this->CI->db->limit(5);
		$query = $this->CI->db->get();
		if($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}
	
	public function get_tieudiem()
	{
		$this->CI->db->select('site_content.id, site_content.alias, site_content.description, site_content.category_id, site_content.title, site_content.status, site_category.id as site_categoryid, site_category.alias as site_categoryalias');
		$this->CI->db->from('site_content','site_category');
		$this->CI->db->join('site_category','site_category.id = site_content.category_id');
		$this->CI->db->where('site_content.status', '1');
		$this->CI->db->where_not_in('site_content.category_id', '4');
		$this->CI->db->where_not_in('site_content.category_id', '2');
		$this->CI->db->order_by('site_content.view', 'DESC');
		$this->CI->db->limit(5);
		
		$query = $this->CI->db->get();
		
		if($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}
	
	public function get_left_sidebar_banner()
	{
		$this->CI->db->select('name, url, image, status');
		$this->CI->db->where('status', '1');
		$this->CI->db->where('type', '2');
		
		$query = $this->CI->db->get('site_banner');

		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();

			return $result;
		}
	}
	
	public function get_left_sidebar_links()
	{
		$this->CI->db->select('name, url, status');
		$this->CI->db->where('status', '1');
		$this->CI->db->where('type', '1');
		$this->CI->db->order_by('id', 'DESC');
		$query = $this->CI->db->get('site_banner');

		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();

			return $result;
		}
	}
	
	
	function get_first_image($content) 
    {
		$regex = "/\<img(.+?)\>/";
		$match = preg_match_all($regex, $content, $matches);
		//var_dump($matches[1]); exit();
		if($match){
		     $temp = $matches[1][0];
		     $temp_arr = explode("src=",$temp);
		  // echo count($temp_arr);exit();
		    echo substr(substr($temp_arr[1],1),0,-3);
		    //echo substr($str,0,-10);
		}
		else
		{
		     echo '<img src="'.base_url().'uploads/images/no-images.jpg" alt="No Image"/>';
		}
    	return $match;
		/*$mateches = array(
                    0 => '<Chi tiết của hình ảnh, bao gồm cả thẻ img, src >',
                    1 => '<Chứa tên hình>'
		)*/
		
        /*global $post, $posts;
        $first_img = '';
        ob_start();
        ob_end_clean();
        $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
        $first_img = $matches [1] [0];
        if(empty($first_img)){ //Defines a default image
        $first_img = "http://a.imageshack.us/img193/8846/uanhincom23102011.jpg";
        }
        return $first_img;*/
    }
}

/* End of file Sidebar_library.php */
/* Location: ./application/libraries/Sidebar_library.php */